


class Dog:
    def __init__(self, name, age):
        self.name = name  # 实例变量
        self.age = age

    def bark(self):
        print(f"{self.name} 在叫！")

# 创建对象（实例）
my_dog = Dog("小白", 3)
my_dog.bark()  # 输出: 小白 在叫！





class Car:
    def __init__(self, brand, color):
        self.brand = brand  # 实例变量
        self.color = color

my_car = Car("Tesla", "red")
print(my_car.brand)  # 输出: Tesla
print(my_car.color)  # 输出: red





class Circle:
    pi = 3.14  # 类变量

    def __init__(self, radius):
        self.radius = radius  # 实例变量

    def area(self):
        return Circle.pi * self.radius ** 2

c = Circle(5)
print(c.area())  # 输出: 78.5





class Student:
    def __init__(self, name):
        self.name = name

    def study(self, subject):
        print(f"{self.name} 正在学习 {subject}")

s = Student("Alice")
s.study("数学")  # 输出: Alice 正在学习 数学





class Pizza:
    toppings = ["cheese"]  # 类变量

    @classmethod
    def add_topping(cls, topping):
        cls.toppings.append(topping)

Pizza.add_topping("pepperoni")
print(Pizza.toppings)  # 输出: ['cheese', 'pepperoni']





class MathUtil:
    @staticmethod
    def add(a, b):
        return a + b

print(MathUtil.add(3, 4))  # 输出: 7





class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # 私有属性

    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount

    def withdraw(self, amount):
        if 0 < amount <= self.__balance:
            self.__balance -= amount

    def get_balance(self):
        return self.__balance

account = BankAccount(1000)
account.deposit(500)
account.withdraw(200)
print(account.get_balance())  # 输出: 1300
# print(account.__balance)  # AttributeError





class Person:
    def __init__(self, name, age):
        self.__name = name
        self.__age = age

    def get_age(self):
        return self.__age

    def set_age(self, age):
        if age > 0:
            self.__age = age
        else:
            print("年龄必须是正数")

p = Person("Bob", 25)
p.set_age(30)
print(p.get_age())  # 输出: 30
p.set_age(-5)  # 输出: 年龄必须是正数





class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print("动物在叫")

class Dog(Animal):
    def speak(self):
        print(f"{self.name} 在汪汪叫")

dog = Dog("小白")
dog.speak()  # 输出: 小白 在汪汪叫





class Cat(Animal):
    def __init__(self, name, color):
        super().__init__(name)  # 调用父类构造函数
        self.color = color

cat = Cat("小花", "white")
print(cat.name, cat.color)  # 输出: 小花 white





class A:
    def method(self):
        print("A 的方法")

class B:
    def method(self):
        print("B 的方法")

class C(A, B):
    pass

c = C()
c.method()  # 输出: A 的方法








class Bird:
    def fly(self):
        print("鸟在飞")

class Penguin(Bird):
    def fly(self):
        print("企鹅不会飞")

def make_fly(animal):
    animal.fly()

b = Bird()
p = Penguin()
make_fly(b)  # 输出: 鸟在飞
make_fly(p)  # 输出: 企鹅不会飞





class Duck:
    def quack(self):
        print("嘎嘎")

class Person:
    def quack(self):
        print("人在模仿鸭叫")

def make_quack(obj):
    obj.quack()

d = Duck()
p = Person()
make_quack(d)  # 输出: 嘎嘎
make_quack(p)  # 输出: 人在模仿鸭叫





from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius ** 2

# s = Shape()  # TypeError: Can't instantiate abstract class
c = Circle(5)
print(c.area())  # 输出: 78.5





class Calculator:
    def add(self, a, b=0, c=0):
        return a + b + c

calc = Calculator()
print(calc.add(1))       # 输出: 1 (a=1, b=0, c=0)
print(calc.add(1, 2))    # 输出: 3 (a=1, b=2, c=0)
print(calc.add(1, 2, 3)) # 输出: 6 (a=1, b=2, c=3)





class Vector:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __add__(self, other):
        return Vector(self.x + other.x, self.y + other.y)

    def __str__(self):
        return f"({self.x}, {self.y})"

v1 = Vector(1, 2)
v2 = Vector(3, 4)
v3 = v1 + v2
print(v3)  # 输出: (4, 6)





class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

    def __str__(self):
        return f"{self.title} by {self.author}"

    def __repr__(self):
        return f"Book('{self.title}', '{self.author}')"

b = Book("Python 101", "John Doe")
print(str(b))  # 输出: Python 101 by John Doe
print(repr(b))  # 输出: Book('Python 101', 'John Doe')





class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __eq__(self, other):
        return self.name == other.name and self.age == other.age

p1 = Person("Alice", 25)
p2 = Person("Alice", 25)
p3 = Person("Bob", 30)
print(p1 == p2)  # 输出: True
print(p1 == p3)  # 输出: False






